package com.zhuoyue.common.utils;

import com.alibaba.fastjson.JSON;
import com.zhuoyue.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
@Component
public class DealLogUtils {
    @Autowired
    private RedisTemplate redisTemplate;

    public void setRedisSourceAndTargetData(String classAndMethodName, SysUser user, Object source, Object target) {
        if (source != null) {
            redisTemplate.opsForValue().set(classAndMethodName + "." + user.getUserId() + ".1", source, 10, TimeUnit.SECONDS);
        }
        if (target != null) {
            redisTemplate.opsForValue().set(classAndMethodName + "." + user.getUserId() + ".2", target, 10, TimeUnit.SECONDS);
        }

    }

    /**
     * 获取原数据
     *
     * @return
     */
    public String getSourceData(String method, Long userId) {
        Object o = redisTemplate.opsForValue().get(method + "." + userId + ".1");
        if (o == null) {
            return null;
        } else {
            return JSON.toJSONString(o);
        }

    }

    /**
     * 获取操作后的数据
     *
     * @return
     */
    public String getTargetData(String method, Long userId) {
        Object o = redisTemplate.opsForValue().get(method + "." + userId + ".2");
        if (o == null) {
            return null;
        } else {
            return JSON.toJSONString(o);
        }
    }
}
